<script lang="ts">
export default {
  inheritAttrs: false,
}

export interface InputProps {
  type: string
  modelValue: string
  shortcut?: string
}
</script>

<script setup lang="ts">
import { ref, onMounted, nextTick, watch } from 'vue'

const props = withDefaults(defineProps<InputProps>(), {
  type: 'text'
})

const input = ref()
const wrapperEl = ref()
const shortcutEl = ref()

defineEmits(['change', 'input', 'update:modelValue'])

const updateShortcutSpacing = () => {
  nextTick(() => {
    if (shortcutEl.value && wrapperEl.value) {
      const shortcutWidth = shortcutEl.value.offsetWidth
      wrapperEl.value.style.setProperty('--shortcut-width', `${shortcutWidth}px`)
    }
  })
}

onMounted(updateShortcutSpacing)
watch(() => props.shortcut, updateShortcutSpacing)

defineExpose({
  focus: () => {
    input.value.focus()
  }
})
</script>

<template>
  <div class="input-wrapper" ref="wrapperEl">
    <slot name="icon" class="icon" />
    <input
      :type="type"
      class="input"
      :class="{'has-icon': $slots.icon, 'has-shortcut': shortcut}"
      ref="input"
      :value="modelValue"
      v-bind="$attrs"
      @input="$emit('update:modelValue', $event.target.value)"
    />
    <kbd v-if="shortcut" class="shortcut" ref="shortcutEl">{{ shortcut }}</kbd>
  </div>
</template>

<style scoped>
.input-wrapper {
  position: relative;
}
.input {
  justify-content: flex-start;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0 10px 0 12px;
  width: 100%;
  height: 40px;
  background-color: var(--vp-c-bg-alt);
  font-size: 14px;
}

.input.has-shortcut {
  padding-right: calc(var(--shortcut-width, 40px) + 22px);
}

.input:hover, .input:focus {
  border-color: var(--vp-c-brand);
  background: var(--vp-c-bg-alt);
}

.input.has-icon {
  padding-left: 52px;
}

.shortcut {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  padding: 2px 6px;
  font-size: 12px;
  font-family: inherit;
  font-weight: 500;
  line-height: 1.5;
  color: var(--vp-c-text-3);
  background: var(--vp-c-default-soft);
  border: 1px solid var(--vp-c-divider);
  border-radius: 4px;
  pointer-events: none;
}

@media (hover: none) {
  .shortcut {
    display: none;
  }
}
</style>

<style>
.input-wrapper svg {
  position: absolute;
  left: 16px;
  top: 12px;
  z-index: 1;
  color: var(--vp-c-text-2);
  pointer-events: none;
}
</style>
